﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using static Umer.HIS.Client.BVL.Common.UserControls.UcToochs;

namespace Umer.HIS.Client.BVL.Common.UserControls
{
    public partial class UcToochs : UserControl
    {
        private Dictionary<string, ToochState> ToochStates = new Dictionary<string, ToochState>();

        private int Cols = 16;
        private int Lines = 2;
        private int Ten_Margin = 5;
        private int Ctrl_Height = 21;

        private int _Width = 0;
        private int _Height = 0;

        private bool bFirst = false;

        private string[,] ToochVal = new string[60, 2] {
                { "RU52,", "RU5,RUE," } ,
                { "RU50,", "RU5," } ,
                { "RU51,", "RUE," } ,
                { "RU42,", "RU4,RUD," } ,
                { "RU40,", "RU4," } ,
                { "RU41,", "RUD," } ,
                { "RU32,", "RU3,RUC," } ,
                { "RU30,", "RU3," } ,
                { "RU31,", "RUC," } ,
                { "RU22,", "RU2,RUB," } ,
                { "RU20,", "RU2," } ,
                { "RU21,", "RUB," } ,
                { "RU12,", "RU1,RUA," } ,
                { "RU10,", "RU1," } ,
                { "RU11,", "RUA," } ,

                { "LU52,", "LU5,LUE," } ,
                { "LU50,", "LU5," } ,
                { "LU51,", "LUE," } ,
                { "LU42,", "LU4,LUD," } ,
                { "LU40,", "LU4," } ,
                { "LU41,", "LUD," } ,
                { "LU32,", "LU3,LUC," } ,
                { "LU30,", "LU3," } ,
                { "LU31,", "LUC," } ,
                { "LU22,", "LU2,LUB," } ,
                { "LU20,", "LU2," } ,
                { "LU21,", "LUB," } ,
                { "LU12,", "LU1,LUA," } ,
                { "LU10,", "LU1," } ,
                { "LU11,", "LUA," } ,

                { "RD52,", "RD5,RDE," } ,
                { "RD50,", "RD5," } ,
                { "RD51,", "RDE," } ,
                { "RD42,", "RD4,RDD," } ,
                { "RD40,", "RD4," } ,
                { "RD41,", "RDD," } ,
                { "RD32,", "RD3,RDC," } ,
                { "RD30,", "RD3," } ,
                { "RD31,", "RDC," } ,
                { "RD22,", "RD2,RDB," } ,
                { "RD20,", "RD2," } ,
                { "RD21,", "RDB," } ,
                { "RD12,", "RD1,RDA," } ,
                { "RD10,", "RD1," } ,
                { "RD11,", "RDA," } ,

                { "LD52,", "LD5,LDE," } ,
                { "LD50,", "LD5," } ,
                { "LD51,", "LDE," } ,
                { "LD42,", "LD4,LDD," } ,
                { "LD40,", "LD4," } ,
                { "LD41,", "LDD," } ,
                { "LD32,", "LD3,LDC," } ,
                { "LD30,", "LD3," } ,
                { "LD31,", "LDC," } ,
                { "LD22,", "LD2,LDB," } ,
                { "LD20,", "LD2," } ,
                { "LD21,", "LDB," } ,
                { "LD12,", "LD1,LDA," } ,
                { "LD10,", "LD1," } ,
                { "LD11,", "LDA," } ,
        };

        //定义事件
        public event UcToochStateChangeHandle UcToochStateChanged;

        //定义委托
        public delegate void UcToochStateChangeHandle(object sender, ToochChangeEvent e);

        public class ToochChangeEvent : EventArgs
        {
            private string message;
            public ToochChangeEvent(string message)
            {
                this.message = message;
            }
            public string Message
            {
                get { return message; }
            }
        }

        public UcToochs()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);//防止多图闪烁
            SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲

            InitToochStates();
        }

        private void InitToochStates()
        {
            ToochStates = new Dictionary<string, ToochState>() {
                { "RU8", new ToochState { Selected = false, Val = "RU8," } },
                { "RU7", new ToochState { Selected = false, Val = "RU7," } },
                { "RU6", new ToochState { Selected = false, Val = "RU6," } },
                { "RU50", new ToochState { Selected = false, Val = "RU5," } },
                { "RU51", new ToochState { Selected = false, Val = "RUE," } },
                { "RU52", new ToochState { Selected = false, Val = "RU5,RUE," } },
                { "RU40", new ToochState { Selected = false, Val = "RU4," } },
                { "RU41", new ToochState { Selected = false, Val = "RUD," } },
                { "RU42", new ToochState { Selected = false, Val = "RU4,RUD," } },
                { "RU30", new ToochState { Selected = false, Val = "RU3," } },
                { "RU31", new ToochState { Selected = false, Val = "RUC," } },
                { "RU32", new ToochState { Selected = false, Val = "RU3,RUC," } },
                { "RU20", new ToochState { Selected = false, Val = "RU2," } },
                { "RU21", new ToochState { Selected = false, Val = "RUB," } },
                { "RU22", new ToochState { Selected = false, Val = "RU2,RUB," } },
                { "RU10", new ToochState { Selected = false, Val = "RU1," } },
                { "RU11", new ToochState { Selected = false, Val = "RUA," } },
                { "RU12", new ToochState { Selected = false, Val = "RU1,RUA," } },

                { "LU8", new ToochState { Selected = false, Val = "LU8," } },
                { "LU7", new ToochState { Selected = false, Val = "LU7," } },
                { "LU6", new ToochState { Selected = false, Val = "LU6," } },
                { "LU50", new ToochState { Selected = false, Val = "LU5," } },
                { "LU51", new ToochState { Selected = false, Val = "LUE," } },
                { "LU52", new ToochState { Selected = false, Val = "LU5,LUE," } },
                { "LU40", new ToochState { Selected = false, Val = "LU4," } },
                { "LU41", new ToochState { Selected = false, Val = "LUD," } },
                { "LU42", new ToochState { Selected = false, Val = "LU4,LUD," } },
                { "LU30", new ToochState { Selected = false, Val = "LU3," } },
                { "LU31", new ToochState { Selected = false, Val = "LUC," } },
                { "LU32", new ToochState { Selected = false, Val = "LU3,LUC," } },
                { "LU20", new ToochState { Selected = false, Val = "LU2," } },
                { "LU21", new ToochState { Selected = false, Val = "LUB," } },
                { "LU22", new ToochState { Selected = false, Val = "LU2,LUB," } },
                { "LU10", new ToochState { Selected = false, Val = "LU1," } },
                { "LU11", new ToochState { Selected = false, Val = "LUA," } },
                { "LU12", new ToochState { Selected = false, Val = "LU1,LUA," } },

                { "RD8", new ToochState { Selected = false, Val = "RD8," } },
                { "RD7", new ToochState { Selected = false, Val = "RD7," } },
                { "RD6", new ToochState { Selected = false, Val = "RD6," } },
                { "RD50", new ToochState { Selected = false, Val = "RD5," } },
                { "RD51", new ToochState { Selected = false, Val = "RDE," } },
                { "RD52", new ToochState { Selected = false, Val = "RD5,RDE," } },
                { "RD40", new ToochState { Selected = false, Val = "RD4," } },
                { "RD41", new ToochState { Selected = false, Val = "RDD," } },
                { "RD42", new ToochState { Selected = false, Val = "RD4,RDD," } },
                { "RD30", new ToochState { Selected = false, Val = "RD3," } },
                { "RD31", new ToochState { Selected = false, Val = "RDC," } },
                { "RD32", new ToochState { Selected = false, Val = "RD3,RDC," } },
                { "RD20", new ToochState { Selected = false, Val = "RD2," } },
                { "RD21", new ToochState { Selected = false, Val = "RDB," } },
                { "RD22", new ToochState { Selected = false, Val = "RD2,RDB," } },
                { "RD10", new ToochState { Selected = false, Val = "RD1," } },
                { "RD11", new ToochState { Selected = false, Val = "RDA," } },
                { "RD12", new ToochState { Selected = false, Val = "RD1,RDA," } },

                { "LD8", new ToochState { Selected = false, Val = "LD8," } },
                { "LD7", new ToochState { Selected = false, Val = "LD7," } },
                { "LD6", new ToochState { Selected = false, Val = "LD6," } },
                { "LD50", new ToochState { Selected = false, Val = "LD5," } },
                { "LD51", new ToochState { Selected = false, Val = "LDE," } },
                { "LD52", new ToochState { Selected = false, Val = "LD5,LDE," } },
                { "LD40", new ToochState { Selected = false, Val = "LD4," } },
                { "LD41", new ToochState { Selected = false, Val = "LDD," } },
                { "LD42", new ToochState { Selected = false, Val = "LD4,LDD," } },
                { "LD30", new ToochState { Selected = false, Val = "LD3," } },
                { "LD31", new ToochState { Selected = false, Val = "LDC," } },
                { "LD32", new ToochState { Selected = false, Val = "LD3,LDC," } },
                { "LD20", new ToochState { Selected = false, Val = "LD2," } },
                { "LD21", new ToochState { Selected = false, Val = "LDB," } },
                { "LD22", new ToochState { Selected = false, Val = "LD2,LDB," } },
                { "LD10", new ToochState { Selected = false, Val = "LD1," } },
                { "LD11", new ToochState { Selected = false, Val = "LDA," } },
                { "LD12", new ToochState { Selected = false, Val = "LD1,LDA," } },
            };
        }

        private void UcToochs_SizeChanged(object sender, EventArgs e)
        {
            FreshCtrl();
        }

        public void Init()
        {
            bFirst = true;

            FreshCtrl();

            bFirst = false;
        }

        public void Init(string val)
        {
            bFirst = true;

            FreshCtrl();

            SetToochState(val);

            bFirst = false;
        }

        private void SetToochState(string val)
        {
            if (string.IsNullOrEmpty(val))
            {
                EmptyToochState();
                return;
            }

            val = val + ",";
            for (int i = 0; i < ToochVal.GetLength(0); i++)
            {
                val = val.Replace(ToochVal[i, 1], ToochVal[i, 0]);
            }
            val = val.TrimEnd(',');

            List<string> states = val.Split(',').ToList();

            foreach (string state in states)
            {
                ToochStates[state].Selected = true;
                int iPos = int.Parse(state.Substring(2, 1));

                string sName  = state.Substring(0, 3);

                getPicByName(sName).BackColor = Color.Red;

                if (iPos < 6)
                {
                    var cbo = getComboBoxByName(sName);
                    cbo.SelectedIndex = int.Parse(state.Substring(3, 1));
                    //cbo.Text = cbo.Items[ii].ToString();
                }
            }
        }

        public void EmptyToochState()
        {
            picRU1.BackColor = Color.White;
            picRU2.BackColor = Color.White;
            picRU3.BackColor = Color.White;
            picRU4.BackColor = Color.White;
            picRU5.BackColor = Color.White;
            picRU6.BackColor = Color.White;
            picRU7.BackColor = Color.White;
            picRU8.BackColor = Color.White;
            picLU1.BackColor = Color.White;
            picLU2.BackColor = Color.White;
            picLU3.BackColor = Color.White;
            picLU4.BackColor = Color.White;
            picLU5.BackColor = Color.White;
            picLU6.BackColor = Color.White;
            picLU7.BackColor = Color.White;
            picLU8.BackColor = Color.White;
            picRD1.BackColor = Color.White;
            picRD2.BackColor = Color.White;
            picRD3.BackColor = Color.White;
            picRD4.BackColor = Color.White;
            picRD5.BackColor = Color.White;
            picRD6.BackColor = Color.White;
            picRD7.BackColor = Color.White;
            picRD8.BackColor = Color.White;
            picLD1.BackColor = Color.White;
            picLD2.BackColor = Color.White;
            picLD3.BackColor = Color.White;
            picLD4.BackColor = Color.White;
            picLD5.BackColor = Color.White;
            picLD6.BackColor = Color.White;
            picLD7.BackColor = Color.White;
            picLD8.BackColor = Color.White;

            cboRU1.SelectedIndex = 0;
            cboRU2.SelectedIndex = 0;
            cboRU3.SelectedIndex = 0;
            cboRU4.SelectedIndex = 0;
            cboRU5.SelectedIndex = 0;

            cboLU1.SelectedIndex = 0;
            cboLU2.SelectedIndex = 0;
            cboLU3.SelectedIndex = 0;
            cboLU4.SelectedIndex = 0;
            cboLU5.SelectedIndex = 0;

            cboRD1.SelectedIndex = 0;
            cboRD2.SelectedIndex = 0;
            cboRD3.SelectedIndex = 0;
            cboRD4.SelectedIndex = 0;
            cboRD5.SelectedIndex = 0;

            cboLD1.SelectedIndex = 0;
            cboLD2.SelectedIndex = 0;
            cboLD3.SelectedIndex = 0;
            cboLD4.SelectedIndex = 0;
            cboLD5.SelectedIndex = 0;

            foreach (var state in ToochStates)
            {
                state.Value.Selected = false;
            }
        }

        private void FreshCtrl()
        {
            if (Width < 325)
            {
                Width = 325;
            }
            if (_Width != Width || bFirst)
            {
                _Width = Width;
                //图片宽
                int picWidth = (Width - Ten_Margin) / Cols;
                //小面板宽
                int pnlWidth = picWidth * 8;

                SetPanalWidth(pnlWidth);

                SetCtrlWidth(picWidth);
            }
            if (Height < 111)
            {
                Height = 111;
            }
            if (_Height != Height || bFirst)
            {
                _Height = Height;
                //图片高
                int picHeight = (Height - Ten_Margin) / Lines - Ctrl_Height;
                //小面板高
                int pnlHeight = picHeight + Ctrl_Height;

                SetPanalHeight(pnlHeight);

                SetCtrlHeight(picHeight);
            }
        }

        private void SetPanalWidth(int iWidth)
        {
            pnlMain.Width = iWidth * 2 + Ten_Margin;
            pnlMain.Left = (Width - pnlMain.Width) / 2;

            pnlRU.Left = 0;
            pnlRU.Width = iWidth;
            pnlRD.Left = 0;
            pnlRD.Width = iWidth;

            pnlLU.Left = iWidth + Ten_Margin;
            pnlLU.Width = iWidth;
            pnlLD.Left = iWidth + Ten_Margin;
            pnlLD.Width = iWidth;
        }

        private void SetPanalHeight(int iHeight)
        {
            pnlMain.Height = iHeight * 2 + Ten_Margin;
            pnlMain.Top = (Height - pnlMain.Height) / 2;

            pnlLU.Height = iHeight;
            pnlLD.Height = iHeight;
            pnlRU.Height = iHeight;
            pnlRD.Height = iHeight;

            pnlRU.Top = 0;
            pnlLU.Top = 0;
            pnlRD.Top = iHeight + Ten_Margin;
            pnlLD.Top = iHeight + Ten_Margin;
        }

        private void SetCtrlWidth(int iWidth)
        {
            picRU8.Left = 0;
            txtRU8.Left = 0;
            picRU8.Width = iWidth;
            txtRU8.Width = iWidth;

            picRU7.Left = picRU8.Left + iWidth;
            txtRU7.Left = picRU7.Left;
            picRU7.Width = iWidth;
            txtRU7.Width = iWidth;

            picRU6.Left = picRU7.Left + iWidth;
            txtRU6.Left = picRU6.Left;
            picRU6.Width = iWidth;
            txtRU6.Width = iWidth;

            picRU5.Left = picRU6.Left + iWidth;
            cboRU5.Left = picRU5.Left;
            picRU5.Width = iWidth;
            cboRU5.Width = iWidth;

            picRU4.Left = picRU5.Left + iWidth;
            cboRU4.Left = picRU4.Left;
            picRU4.Width = iWidth;
            cboRU4.Width = iWidth;

            picRU3.Left = picRU4.Left + iWidth;
            cboRU3.Left = picRU3.Left;
            picRU3.Width = iWidth;
            cboRU3.Width = iWidth;

            picRU2.Left = picRU3.Left + iWidth;
            cboRU2.Left = picRU2.Left;
            picRU2.Width = iWidth;
            cboRU2.Width = iWidth;

            picRU1.Left = picRU2.Left + iWidth;
            cboRU1.Left = picRU1.Left;
            picRU1.Width = iWidth;
            cboRU1.Width = iWidth;

            picRD8.Left = 0;
            txtRD8.Left = 0;
            picRD8.Width = iWidth;
            txtRD8.Width = iWidth;

            picRD7.Left = picRD8.Left + iWidth;
            txtRD7.Left = picRD7.Left;
            picRD7.Width = iWidth;
            txtRD7.Width = iWidth;

            picRD6.Left = picRD7.Left + iWidth;
            txtRD6.Left = picRD6.Left;
            picRD6.Width = iWidth;
            txtRD6.Width = iWidth;

            picRD5.Left = picRD6.Left + iWidth;
            cboRD5.Left = picRD5.Left;
            picRD5.Width = iWidth;
            cboRD5.Width = iWidth;

            picRD4.Left = picRD5.Left + iWidth;
            cboRD4.Left = picRD4.Left;
            picRD4.Width = iWidth;
            cboRD4.Width = iWidth;

            picRD3.Left = picRD4.Left + iWidth;
            cboRD3.Left = picRD3.Left;
            picRD3.Width = iWidth;
            cboRD3.Width = iWidth;

            picRD2.Left = picRD3.Left + iWidth;
            cboRD2.Left = picRD2.Left;
            picRD2.Width = iWidth;
            cboRD2.Width = iWidth;

            picRD1.Left = picRD2.Left + iWidth;
            cboRD1.Left = picRD1.Left;
            picRD1.Width = iWidth;
            cboRD1.Width = iWidth;

            picLU1.Left = 0;
            cboLU1.Left = 0;
            picLU1.Width = iWidth;
            cboLU1.Width = iWidth;

            picLU2.Left = picLU1.Left + iWidth;
            cboLU2.Left = picLU2.Left;
            picLU2.Width = iWidth;
            cboLU2.Width = iWidth;

            picLU3.Left = picLU2.Left + iWidth;
            cboLU3.Left = picLU3.Left;
            picLU3.Width = iWidth;
            cboLU3.Width = iWidth;

            picLU4.Left = picLU3.Left + iWidth;
            cboLU4.Left = picLU4.Left;
            picLU4.Width = iWidth;
            cboLU4.Width = iWidth;

            picLU5.Left = picLU4.Left + iWidth;
            cboLU5.Left = picLU5.Left;
            picLU5.Width = iWidth;
            cboLU5.Width = iWidth;

            picLU6.Left = picLU5.Left + iWidth;
            txtLU6.Left = picLU6.Left;
            picLU6.Width = iWidth;
            txtLU6.Width = iWidth;

            picLU7.Left = picLU6.Left + iWidth;
            txtLU7.Left = picLU7.Left;
            picLU7.Width = iWidth;
            txtLU7.Width = iWidth;

            picLU8.Left = picLU7.Left + iWidth;
            txtLU8.Left = picLU8.Left;
            picLU8.Width = iWidth;
            txtLU8.Width = iWidth;

            picLD1.Left = 0;
            cboLD1.Left = 0;
            picLD1.Width = iWidth;
            cboLD1.Width = iWidth;

            picLD2.Left = picLD1.Left + iWidth;
            cboLD2.Left = picLD2.Left;
            picLD2.Width = iWidth;
            cboLD2.Width = iWidth;

            picLD3.Left = picLD2.Left + iWidth;
            cboLD3.Left = picLD3.Left;
            picLD3.Width = iWidth;
            cboLD3.Width = iWidth;

            picLD4.Left = picLD3.Left + iWidth;
            cboLD4.Left = picLD4.Left;
            picLD4.Width = iWidth;
            cboLD4.Width = iWidth;

            picLD5.Left = picLD4.Left + iWidth;
            cboLD5.Left = picLD5.Left;
            picLD5.Width = iWidth;
            cboLD5.Width = iWidth;

            picLD6.Left = picLD5.Left + iWidth;
            txtLD6.Left = picLD6.Left;
            picLD6.Width = iWidth;
            txtLD6.Width = iWidth;

            picLD7.Left = picLD6.Left + iWidth;
            txtLD7.Left = picLD7.Left;
            picLD7.Width = iWidth;
            txtLD7.Width = iWidth;

            picLD8.Left = picLD7.Left + iWidth;
            txtLD8.Left = picLD8.Left;
            picLD8.Width = iWidth;
            txtLD8.Width = iWidth;
        }

        private void SetCtrlHeight(int iHeight)
        {
            picLU8.Height = iHeight;
            picLU7.Height = iHeight;
            picLU6.Height = iHeight;
            picLU5.Height = iHeight;
            picLU4.Height = iHeight;
            picLU3.Height = iHeight;
            picLU2.Height = iHeight;
            picLU1.Height = iHeight;

            picRU8.Height = iHeight;
            picRU7.Height = iHeight;
            picRU6.Height = iHeight;
            picRU5.Height = iHeight;
            picRU4.Height = iHeight;
            picRU3.Height = iHeight;
            picRU2.Height = iHeight;
            picRU1.Height = iHeight;

            txtLU8.Top = iHeight;
            txtLU7.Top = iHeight;
            txtLU6.Top = iHeight;
            cboLU5.Top = iHeight;
            cboLU4.Top = iHeight;
            cboLU3.Top = iHeight;
            cboLU2.Top = iHeight;
            cboLU1.Top = iHeight;

            txtRU8.Top = iHeight;
            txtRU7.Top = iHeight;
            txtRU6.Top = iHeight;
            cboRU5.Top = iHeight;
            cboRU4.Top = iHeight;
            cboRU3.Top = iHeight;
            cboRU2.Top = iHeight;
            cboRU1.Top = iHeight;

            picLD8.Height = iHeight;
            picLD7.Height = iHeight;
            picLD6.Height = iHeight;
            picLD5.Height = iHeight;
            picLD4.Height = iHeight;
            picLD3.Height = iHeight;
            picLD2.Height = iHeight;
            picLD1.Height = iHeight;

            picRD8.Height = iHeight;
            picRD7.Height = iHeight;
            picRD6.Height = iHeight;
            picRD5.Height = iHeight;
            picRD4.Height = iHeight;
            picRD3.Height = iHeight;
            picRD2.Height = iHeight;
            picRD1.Height = iHeight;

            //txtLD8.Top = iHeight;
            //txtLD7.Top = iHeight;
            //txtLD6.Top = iHeight;
            //cboLD5.Top = iHeight;
            //cboLD4.Top = iHeight;
            //cboLD3.Top = iHeight;
            //cboLD2.Top = iHeight;
            //cboLD1.Top = iHeight;

            //txtRD8.Top = iHeight;
            //txtRD7.Top = iHeight;
            //txtRD6.Top = iHeight;
            //cboRD5.Top = iHeight;
            //cboRD4.Top = iHeight;
            //cboRD3.Top = iHeight;
            //cboRD2.Top = iHeight;
            //cboRD1.Top = iHeight;

        }

        private void UcToochs_Resize(object sender, EventArgs e)
        {
            FreshCtrl();
        }

        private void Pic_Click(object sender, EventArgs e)
        {
            string sName = ((PictureBox)sender).Name;
            sName = sName.Replace("pic", "");

            int ii = int.Parse(sName.Substring(2, 1));

            if ((((PictureBox)sender).BackColor == Color.White))
            {
                ((PictureBox)sender).BackColor = Color.Red;
                if (ii < 6)
                {
                    ToochStates[sName + "0"].Selected = false;
                    ToochStates[sName + "1"].Selected = false;
                    ToochStates[sName + "2"].Selected = false;

                    ComboBox cbo = getComboBoxByName(sName);
                    int index = cbo.Items.IndexOf(cbo.Text);

                    ToochStates[sName + index.ToString()].Selected = true;

                    //  getCboByName(sName).Text = ii.ToString(); 
                }
                else
                {
                    ToochStates[sName].Selected = true;
                }
            }
            else
            {
                ((PictureBox)sender).BackColor = Color.White;
                if (ii < 6)
                {
                    ToochStates[sName + "0"].Selected = false;
                    ToochStates[sName + "1"].Selected = false;
                    ToochStates[sName + "2"].Selected = false;
                }
                else
                {
                    ToochStates[sName].Selected = false;
                }

            }

            if (UcToochStateChanged != null)
            {
                UcToochStateChanged(this, new ToochChangeEvent(GetToochState()));
            }
        }

        public string GetToochState()
        {
            string states = "";

            foreach (var state in ToochStates)
            {
                if(state.Value.Selected)
                {
                    states += state.Value.Val;
                }
            }

            return states.TrimEnd(',');
        }

        private ComboBox getComboBoxByName(string sName)
        {
            if (sName == "RU1") return cboRU1;
            if (sName == "RU2") return cboRU2;
            if (sName == "RU3") return cboRU3;
            if (sName == "RU4") return cboRU4;
            if (sName == "RU5") return cboRU5;

            if (sName == "LU1") return cboLU1;
            if (sName == "LU2") return cboLU2;
            if (sName == "LU3") return cboLU3;
            if (sName == "LU4") return cboLU4;
            if (sName == "LU5") return cboLU5;

            if (sName == "RD1") return cboRD1;
            if (sName == "RD2") return cboRD2;
            if (sName == "RD3") return cboRD3;
            if (sName == "RD4") return cboRD4;
            if (sName == "RD5") return cboRD5;

            if (sName == "LD1") return cboLD1;
            if (sName == "LD2") return cboLD2;
            if (sName == "LD3") return cboLD3;
            if (sName == "LD4") return cboLD4;
            if (sName == "LD5") return cboLD5;

            return null;
        }

        //private TextBox getTextBoxByName(string sName)
        //{
        //    if (sName == "RD6") return txtRD6;
        //    if (sName == "RD7") return txtRD7;
        //    if (sName == "RD8") return txtRD8;

        //    if (sName == "LU6") return txtLU6;
        //    if (sName == "LU7") return txtLU7;
        //    if (sName == "LU8") return txtLU8;

        //    if (sName == "RU6") return txtRU6;
        //    if (sName == "RU7") return txtRU7;
        //    if (sName == "RU8") return txtRU8;

        //    if (sName == "LD6") return txtLD6;
        //    if (sName == "LD7") return txtLD7;
        //    if (sName == "LD8") return txtLD8;

        //    return null;
        //}

        private PictureBox getPicByName(string sName)
        {
            if (sName == "RU1") return picRU1;
            if (sName == "RU2") return picRU2;
            if (sName == "RU3") return picRU3;
            if (sName == "RU4") return picRU4;
            if (sName == "RU5") return picRU5;
            if (sName == "RU6") return picRU6;
            if (sName == "RU7") return picRU7;
            if (sName == "RU8") return picRU8;
            if (sName == "LU1") return picLU1;
            if (sName == "LU2") return picLU2;
            if (sName == "LU3") return picLU3;
            if (sName == "LU4") return picLU4;
            if (sName == "LU5") return picLU5;
            if (sName == "LU6") return picLU6;
            if (sName == "LU7") return picLU7;
            if (sName == "LU8") return picLU8;
            if (sName == "RD1") return picRD1;
            if (sName == "RD2") return picRD2;
            if (sName == "RD3") return picRD3;
            if (sName == "RD4") return picRD4;
            if (sName == "RD5") return picRD5;
            if (sName == "RD6") return picRD6;
            if (sName == "RD7") return picRD7;
            if (sName == "RD8") return picRD8;
            if (sName == "LD1") return picLD1;
            if (sName == "LD2") return picLD2;
            if (sName == "LD3") return picLD3;
            if (sName == "LD4") return picLD4;
            if (sName == "LD5") return picLD5;
            if (sName == "LD6") return picLD6;
            if (sName == "LD7") return picLD7;
            if (sName == "LD8") return picLD8;

            return null;
        }

        private void ComboBox_Click(object sender, EventArgs e)
        {
            ((ComboBox)sender).DropDownStyle = ComboBoxStyle.DropDownList;
        }

        private void ComboBox_Leave(object sender, EventArgs e)
        {
            ((ComboBox)sender).DropDownStyle = ComboBoxStyle.Simple;
        }

        private void ComboBox_DropDownClosed(object sender, EventArgs e)
        {
            ((ComboBox)sender).DropDownStyle = ComboBoxStyle.Simple;

            int ii = ((ComboBox)sender).Items.IndexOf(((ComboBox)sender).Text);

            string sName = ((ComboBox)sender).Name;
            sName = sName.Replace("cbo", "");

            getPicByName(sName).BackColor = Color.Red;

            ToochStates[sName + "0"].Selected = false;
            ToochStates[sName + "1"].Selected = false;
            ToochStates[sName + "2"].Selected = false;

            if(ii>-1) ToochStates[sName + ii.ToString()].Selected = true;

            if (UcToochStateChanged != null)
            {
                UcToochStateChanged(this, new ToochChangeEvent(GetToochState()));
            }
        }
    }

    public class ToochState
    {
        public bool Selected { get; set; }
        public string Val { get; set; }
    }
}
